package com.liujun.spring4.demo.aop.ioc.base.beans;

import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;

import java.beans.PropertyDescriptor;

/**
 * @author liujun
 * @version 1.0
 * @date 2018-08-13 07:24:00
 */
public class MyInstantiationAwareBeanPostProcessor
    extends InstantiationAwareBeanPostProcessorAdapter {

  /**
   * 1，接口方法，在实例化bean前调用
   *
   * @param beanClass
   * @param beanName
   * @return
   * @throws BeansException
   */
  @Override
  public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName)
      throws BeansException {

    // 仅对容器中的car bean处理
    if ("car".equals(beanName)) {
      System.out.println("InstantiationAware BeanPostProcessor.postProcessBeforeInstantiation");
    }

    return null;
  }

  /**
   * 接口方法，在实例化bean后调用
   *
   * @param bean
   * @param beanName
   * @return
   * @throws BeansException
   */
  @Override
  public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
    // 仅对容器中的carbean进行处理
    if ("car".equals(beanName)) {
      System.out.println("InstantiationAware BeanPostProcessor.postProcessAfterInstantiation");
    }

    return true;
  }

  /**
   * 3,接口方法，在设置某个属性时调用
   *
   * @param pvs
   * @param pds
   * @param bean
   * @param beanName
   * @return
   * @throws BeansException
   */
  @Override
  public PropertyValues postProcessPropertyValues(
      PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName)
      throws BeansException {

    // 仅对容器中的carBean进行处理，还可以通过pdst入参进行过滤,仅对car的某个特定属性值进行处理
    if ("car".equals(beanName)) {
      System.out.println("Instation AwareBeanPostProcess.postProcessPropertyValues");
    }

    return pvs;
  }


}
